package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.mapper.DishMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author CLINCH
* @description 针对表【dish(菜品管理)】的数据库操作Service实现
* @createDate 2023-02-28 09:39:56
*/
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
    implements DishService{
    @Autowired
    private DishFlavorService dishFlavorService;

    @Transactional
    public void saveOrUpdateWithFlavor(DishDto dishDto){
        //保存菜品的基本信息到菜品表
        this.saveOrUpdate(dishDto);
        Long disId = dishDto.getId(); //菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(disId);
            return item;
        }).collect(Collectors.toList());
        //保存菜品口味数据到菜品口味表dis_flavor
        dishFlavorService.saveOrUpdateBatch(dishDto.getFlavors());

    }

    @Transactional
    public void deleteDishById(List<Long> ids){
        //更新dish表的is_delete
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getId,ids);
        Dish dish = new Dish();
        dish.setIsDeleted(1);
        //this.up(dish,dishLambdaQueryWrapper);
        //更新dishFlavor表的is_delete
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,ids);
        DishFlavor dishFlavor = new DishFlavor();
        dishFlavor.setIsDeleted(1);
        dishFlavorService.update(dishFlavor,dishFlavorLambdaQueryWrapper);


    }


}




